<?php

class Person extends Controller 
{
	var $uid;
	var $tpl;
    var $dbdata;

	function setVars()
	{
		$this->tpl['title'] = "Personal info";
		$this->tpl['head'] = '';
        $this->tpl['sidebar'] = 'As Work Phone Number you may add an extention.<br />Format: (800) 555-1212 x345';
        $this->uid = $this->session->userdata('uid');
        $this->pid = $this->session->userdata('people_id');
        $this->getDB();
	}
	
	function __construct()
	{
		parent::Controller();
        log_message('debug', "Contact->person: Controler Class Initialized");
        $this->auth->CheckLogin();
		$this->setVars();
        //$this->output->enable_profiler(TRUE);
	}

	function index() 
	{
		if(isset($_POST['submit_pi']))
		{
			$this->postCheck();
		}
		else
		{
           $tpl = (array) $this->dbdata;
           $this->tpl = array_merge($this->tpl, $tpl);
           $this->load->view('contact/person', $this->tpl);
		}
	}

    function getDB()
    {
        //$this->db->select('Honorific, First_Name, Middle_Name');
        $query = $this->db->getwhere('contact_people', array('id' => $this->pid), 1);
        if($query->num_rows() > 0) {
            $dbdata = $query->row();
            unset($dbdata->Country, $dbdata->eMail);
            $this->dbdata = delids($dbdata);
            $dbdata = (array) $this->dbdata;
            $this->tpl = array_merge($this->tpl, $dbdata);
            $this->tpl['select_Honorific'] = honorific_form($dbdata['Honorific']);
        }
    }

	function postCheck()
	{
		log_message('debug', "Conatct Person: Post check");
		$this->load->library('validation');
        $rules['Honorific'] = 'trim|required|xss_clean';
        $rules['First_Name'] = 'trim|required|min_length[2]|max_length[30]|alpha_numeric|xss_clean';
        $rules['Middle_Name'] = 'trim|max_length[30]|alpha_numeric|xss_clean';
        $rules['Last_Name'] = 'trim|required|min_length[2]|max_length[30]|alpha_numeric|xss_clean';
        $rules['Postcode'] = 'trim|required|min_length[4]|max_length[15]|alpha_numeric|xss_clean';
        $rules['Street'] = 'trim|required|min_length[5]|max_length[50]|xss_clean';
        $rules['Cell_Phone'] = 'trim|max_length[15]|xss_clean';
        $rules['Home_Phone'] = 'trim|max_length[15]|xss_clean';
        $rules['Work_Phone'] = 'trim|max_length[23]|xss_clean';
        $rules['Date_of_Birth'] = 'trim|required|min_length[5]|max_length[20]|xss_clean';
        $rules['Website'] = 'trim|xss_clean|prep_url';
		$this->validation->set_rules($rules);
		//$this->validation->set_fields($fields);
		log_message('debug', "Register: Validation Rules set");
		if ($this->validation->run() == FALSE)
		{
            $this->Honorific = $this->validation->Honorific;
            $this->tpl['select_Honorific'] = honorific_form($this->Honorific);
            foreach($this->dbdata as $k => $v){
                if(isset($this->validation->$k)){
                    $this->tpl[$k] = $this->validation->$k;
                }
            }            
			$this->load->view('contact/person', $this->tpl);
		}
		else
		{
            $data = (array) $this->dbdata;
            foreach($this->dbdata as $k => $v){
                if(isset($this->validation->$k)){
                    if(strstr($k, 'Phone') && isset($this->validation->$k)){
                        $this->validation->$k = $this->fun->format_phone($this->validation->$k);
                    }
                    $data[$k] = $this->validation->$k;
                }else{
                    $data[$k] = '';
                }
            }
            $this->load->library('dbStringIt');
            $dbd = (array) $this->dbdata;
            $data = new_value($dbd, $data);
            if(count($data) != 0){
                $this->db->where('id', $this->pid);
                $this->db->update('contact_people', $data);
                $this->msg->setMsg('Updated');
            }else{
                $this->msg->setError('Nothing to update');
            }
            $this->tpl = array_merge($this->tpl, $dbd);
            $this->tpl = array_merge($this->tpl, $data);
            $this->tpl['select_Honorific'] = honorific_form($this->tpl['Honorific']);
            $this->load->view('contact/person', $this->tpl);
            //redirect('contact/person', 'location');
		}
	}
    

}
?>
